package com.zhangh.summer.admin.controller.login;

import com.zhangh.summer.admin.biz.login.LoginBiz;
import com.zhangh.summer.admin.biz.menu.MenuBiz;
import com.zhangh.summer.admin.model.bo.LoginBO;
import com.zhangh.summer.admin.model.bo.MenuBO;
import com.zhangh.summer.admin.model.bo.SessionUserBO;
import com.zhangh.summer.admin.support.base.JsonResponse;
import com.zhangh.summer.admin.support.session.SessionUser;
import com.zhangh.summer.admin.support.session.SysSession;
import com.zhangh.summer.admin.support.util.IpGetter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2021-10-09 14:00
 **/
@RestController
public class LoginController {
    @Autowired
    private LoginBiz loginBiz;
    @Autowired
    private MenuBiz menuBiz;

    @RequestMapping("/login")
    public String login(HttpServletRequest request,@RequestBody LoginBO loginBO) {
        /*String ip= IpGetter.getRequestIp(request);
        loginBO.setIp(ip);
        SessionUser sessionUser=loginBiz.login(loginBO);
        return new JsonResponse().toJson(sessionUser);*/
        String ip= IpGetter.getRequestIp(request);
        loginBO.setIp(ip);
        SessionUserBO sessionUserBO=loginBiz.loginWithMenu(loginBO);
        return new JsonResponse().toJson(sessionUserBO);
    }
    @RequestMapping("/getServerTime")
    public String getServerTime() {
        long time=System.currentTimeMillis();
        return new JsonResponse().toJson(time);
    }

    @RequestMapping("/getCurrentMenu")
    public String getUserAuthMenu() {
        Long userID= SysSession.getUser().getUserID();
        List<MenuBO> menuBOList=this.menuBiz.getUserAuthMenuList(userID);
        return new JsonResponse().toJson(menuBOList);
    }

    @RequestMapping("/loginOut")
    public String loginOut() {
        String token= SysSession.getUser().getToken();
        this.loginBiz.loginOut(token);
        return new JsonResponse().toJson();
    }

    @RequestMapping("/loginCheck")
    public String loginCheck(@RequestBody Map params) {
        String token= params.get("token")!=null?params.get("token").toString():null;
        boolean flag=this.loginBiz.loginCheck(token);
        return new JsonResponse(flag).toJson(flag);
    }




}
